package com.lagou.controller;

import com.lagou.pojo.Products;
import com.lagou.pojo.ProductsVo;
import com.lagou.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 5:31 下午 2021/9/14
 */

@RestController
@RequestMapping("/product")
public class ProductsController {
    @Autowired
    private ProductsService productsService;

    @GetMapping("/query/{id}")
    public Products query(@PathVariable Integer id) {
        return productsService.findById(id);
    }

    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable Integer id) {
        return productsService.deleteById(id);
    }

    @PostMapping("/saveOrUpdate")
    public int saveOrUpdate(@RequestBody Products products) {
        int i = 0;
        //根据id判断新增或更新
        if (products.getId() == null) {
            i = productsService.addProducts(products);
        } else {
            i = productsService.updateProducts(products);

        }
        return i;
    }

    @PostMapping("/queryByPage")
    public Map<String, Object> queryByPage(@RequestBody ProductsVo productsVo) {
        Map<String, Object> map = productsService.selectPage(productsVo);
        return map;
    }

    @GetMapping("/queryGoodsList/{id}")
    public List<Products> queryGoodsList(@PathVariable Integer id){
      /*  try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //根据订单的id在中间表中查询商品的id
        List<Integer> pIds = productsService.selectMiddlePidsByOid(id);
        //根据商品的id集合查询商品的信息
        List<Products> list = productsService.queryGoodsList(pIds);
        return list;
    }
}
